home *** CD-ROM | disk | FTP | other *** search
- /* ------------------------------------------------------------------------------
-
- FILENAME
- 4-Up.c
-
- DESCRIPTION
- This file contains the code for a Printing Extension which does 4-up printing.
-
- COPYRIGHT
- Copyright Apple Computer, Inc. 1993
- All rights reserved.
-
- MODIFICATION HISTORY
- 04/7/93 Sam Weiss Initial Implementation
-
-
- ------------------------------------------------------------------------------- */
-
-
- #include <Types.h>
- #include <Exceptions.h>
- #include <PrintingManager.h>
- #include <PrintingMessages.h>
- #include <graphics routines.h>
- #include <math routines.h>
-
-
- // ------------------------------------------------------------------------------
-
-
- OSErr FourUpCountPages ( gxSpoolFile thePrintFile, long* numPages ) {
-
- OSErr anErr;
- long originalPages;
-
- anErr = Forward_GXCountPages(thePrintFile, &originalPages);
- nrequire (anErr, FailedForward_GXCountPages);
-
- *numPages = (originalPages + 3) / 4;
-
- FailedForward_GXCountPages:
- return anErr;
- }
-
-
- // ------------------------------------------------------------------------------
-
-
- OSErr FourUpDespoolPage ( gxSpoolFile thePrintFile, long pageNumber,
- gxFormat pageFormat, gxShape* pagePicture,
- Boolean* formatChanged ) {
-
- OSErr anErr;
- long firstPage, lastPage, numPages, whichPage;
- gxShape fourUpPage;
- gxShape thePages[4];
- gxShape* atPage;
-
- // determine actual page number of pages to despool
-
- lastPage = pageNumber * 4;
- firstPage = lastPage - 3;
-
- // determine page number for last page in spool file,
- // so we can constrain our despooling loop to a valid range
- // if less than four pages remain in the file
-
- anErr = ForwardMessage(gxCountPages, thePrintFile, &numPages);
- nrequire (anErr, FailedForward_GXCountPages);
-
- if (lastPage > numPages)
- lastPage = numPages;
-
- // create picture shape to hold sub-pages
-
- fourUpPage = GXNewShape(gxPictureType);
- anErr = GXGetGraphicsError(nil);
- nrequire (anErr, FailedGXNewShape);
-
- // despool backwards so pageFormat ends up containing
- // the format for the first page in the group
-
- atPage = &thePages[lastPage-firstPage]; // point to last page in group
- numPages = 0; // track number of successfully despooled pages
-
- for (whichPage = lastPage; whichPage >= firstPage; --whichPage) {
- anErr = Forward_GXDespoolPage (thePrintFile, whichPage, pageFormat, atPage--, formatChanged);
- nrequire (anErr, FailedForward_GXDespoolPage);
- ++numPages;
- }
-
- // map and translate the despooled pages onto a single physical page
-
- {
- gxRectangle pageRect;
- fixed tx, ty;
- gxMapping aMapping;
-
- // get the dimensions of the physical page
-
- GXGetFormatDimensions(pageFormat, &pageRect, nil);
-
- // compute x and y translation factors
-
- tx = (pageRect.right - pageRect.left) >> 1;
- ty = (pageRect.bottom - pageRect.top) >> 1;
-
- // initialize the mapping
-
- ResetMapping(&aMapping);
- aMapping.map[0][0] = fixed1/2;
- aMapping.map[1][1] = fixed1/2;
-
- // map the pages onto the physical page
-
- GXMapShape(thePages[0], &aMapping);
-
- if (numPages > 1) {
- MoveMapping(&aMapping, tx, 0);
- GXMapShape(thePages[1], &aMapping);
- if (numPages > 2) {
- MoveMapping(&aMapping, -tx, ty);
- GXMapShape(thePages[2], &aMapping);
- if (numPages > 3) {
- MoveMapping(&aMapping, tx, 0);
- GXMapShape(thePages[3], &aMapping);
- }
- }
- }
-
- // place the mapped pages into a single picture
-
- GXSetPictureParts(fourUpPage, 1, 0, numPages, thePages, nil, nil, nil);
- anErr = GXGetGraphicsError(nil);
- nrequire (anErr, FailedGXSetPictureParts);
-
- // GXSetPictureParts cloned the pages,
- // so we must dispose our references to them
-
- for (atPage = &thePages[numPages-1]; atPage >= thePages; --atPage)
- GXDisposeShape(*atPage);
- }
-
- // return the 4-up page
-
- *pagePicture = fourUpPage;
-
- // since we don't know whether the format for
- // "actual page number 5" is the same as that for
- // "actual page number 1", we always set formatChanged to true
-
- *formatChanged = true;
-
- ncheck (anErr);
- return noErr;
-
- //------------------------
- // exception handling code
- //------------------------
-
- FailedGXSetPictureParts:
- FailedForward_GXDespoolPage:
- for (atPage = &thePages[numPages-1]; atPage >= thePages; --atPage)
- GXDisposeShape(*atPage);
- GXDisposeShape(fourUpPage);
- FailedGXNewShape:
- FailedForward_GXCountPages:
- return anErr;
- }
-
-
- // ------------------------------------------------------------------------------
-